import ubx
import serial
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)
ax.grid()
xdata, ydata = [], []
ubx_stream = ubx.UbxStream(serial.Serial(port='com6', baudrate=115200))

def data_gen(t=0):
    cnt = 0
    while True:
        asmaa = ubx_stream.read()
        print(asmaa.lat/10000000)
        print(asmaa.lon/10000000)
        print(asmaa.vAcc/1000)
        yield asmaa.lat/10000000, asmaa.lon/10000000


def init():
    ax.set_ylim(121.2472, 121.2476)
    ax.set_xlim(30.875, 30.876)
    del xdata[:]
    del ydata[:]
    line.set_data(xdata, ydata)
    return line,

def run(data):
    # update the data
    t, y = data
    xdata.append(t)
    ydata.append(y)
    xmin, xmax = ax.get_xlim()

    if t >= xmax:
        ax.set_xlim(xmin, xmax)
        ax.figure.canvas.draw()
    line.set_data(xdata, ydata)

    return line,

ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,
                              repeat=False, init_func=init)
plt.show()

# import geopy.distance
# geopy.distance.distance((31.0924595, 121.347275),(31.0924595, 121.34723)).m